﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CopyData.cs" company="">
//   
// </copyright>
// <summary>
//   The copy data.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Core
{
    using System.Collections.Generic;

    /// <summary>
    ///   The copy data.
    /// </summary>
    public class CopyData : ICopyData
    {
        #region Fields

        /// <summary>
        ///   The _items.
        /// </summary>
        private readonly Dictionary<string, ICopyValue> _items = new Dictionary<string, ICopyValue>();

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets the formats.
        /// </summary>
        public IEnumerable<string> Formats
        {
            get
            {
                return this._items.Keys;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The get value.
        /// </summary>
        /// <param name="format">
        /// The format. 
        /// </param>
        /// <param name="action">
        /// The action. 
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="T"/> . 
        /// </returns>
        /// <exception cref="NotExistsException">
        /// </exception>
        public T GetValue<T>(string format, NotExistsAction action = NotExistsAction.ThrowException)
        {
            if (this._items.ContainsKey(format))
            {
                return (T)this._items[format].GetValue();
            }

            if (action == NotExistsAction.ThrowException)
            {
                throw new NotExistsException("Data in format <" + format + "> doesn't exist");
            }

            return default(T);
        }

        /// <summary>
        /// The is exist.
        /// </summary>
        /// <param name="format">
        /// The format. 
        /// </param>
        /// <returns>
        /// The <see cref="bool"/> . 
        /// </returns>
        public bool IsExist(string format)
        {
            return this._items.ContainsKey(format);
        }

        /// <summary>
        /// The set value.
        /// </summary>
        /// <param name="format">
        /// The format. 
        /// </param>
        /// <param name="value">
        /// The value. 
        /// </param>
        public void SetValue(string format, ICopyValue value)
        {
            this._items[format] = value;
        }

        #endregion
    }
}